/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import entidad.Actor;
import entidad.Artist;
import entidad.Song;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JOptionPane;

/**
 *
 * @author Juan
 */
public class SongDAO {

    public boolean crear(Song song) {
        boolean respuesta=false;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(song);
            em.getTransaction().commit();
            respuesta=true;
        } catch (Exception e) {
            em.getTransaction().rollback();
            JOptionPane.showMessageDialog(null, e);
        } finally {
            em.close();
            return respuesta;
        }
    }
    
    public boolean setArtist(int songId, int artistId) {
       boolean respuesta=false;
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
       EntityManager em = emf.createEntityManager();
       em.getTransaction().begin();
       Song song = em.find(Song.class, songId);
       Artist artist = em.find(Artist.class, artistId);
       artist.addSong(song);
       return true;
    }
}
    